#include <string.h>
#include "uart.h"
#include <stdio.h>
#include <unistd.h>
#include "car.h"
#include "file.h"
#include "tcpcline.h"
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>

#define		ENABLE_DEBUG	0

char b[] = {0xaa,0xab,0x03,0x2e,0x10,0x27};
int tcp,cli;

void usart_data_handle( unsigned char * data , int num )
{
	
	char carId[15] = {0};
#if ENABLE_DEBUG
	printf("串口收到数据\n");
	printf("长度：%d\n",num);
	printf("数据:\n");
	for(int i = 0 ; i < num ; i ++ )
	{
		printf("%x\n",data[i]);
	}
#endif
	if( num < 5 ) return ;
	switch( data[3] )
	{
		case 0x90 :
		{
			jiaoyan(read_cmd,9);
			write(uart,read_cmd,sizeof(read_cmd));
			break;
		}
		case 0x91 :
		{
			//write_car_id("川A:12345");
			//return;
			get_car_id(data+5,carId,10);
			save_car_msg(carId);
			char a[15];
			sprintf(a,"%s%02d%02d%d",carId,car_money,car_pay,car_flag);
			write(cli,a,15);
			break;
			
		}
	}
}


int main(void)
{
	int num = 0;
	
	unsigned char buf[1024] = {0};
	tcp = init_tcp("172.16.50.122",10020);
	usart_init();
	if(uart <= 0 ) return 0;
	while(1)
	{
		printf("Wait for connect..\r\n");
		cli = accept(tcp,NULL,NULL);
		printf("Connect success\n\r\n");
		while(1)
		{
			num = read(uart,buf,1024);
			if( num > 0 ) 
			{
				usart_data_handle(buf,num);
			}	
		}
		
	}
	return 0;
}



